package com.bw.home.ui

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.SizeUtils
import com.bw.common.BaseApplication
import com.bw.common.db.entity.HistoryEntity
import com.bw.common.utils.LogUtils
import com.bw.home.R
import com.bw.home.databinding.ActivitySearchBinding
import com.bw.mvicore.ui.BaseActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext

class SearchActivity : BaseActivity() {
    private val TAG = SearchActivity::class.simpleName
    private lateinit var binding: ActivitySearchBinding
    override fun releaseRes() {

    }

    override fun generateContentView(): View {
        binding = ActivitySearchBinding.inflate(layoutInflater)
        return binding.root
    }

    private val padding = SizeUtils.dp2px(3.0F)
    override fun initView() {
        super.initView()
        binding.flHomeSearchHot.removeAllViews()
        val hotSearch = resources.getStringArray(R.array.hotSearch)
        hotSearch.forEach {
            binding.flHomeSearchHot.addView(generateTextView(it))
        }
    }

    override fun initData() {
        lifecycleScope.launch(Dispatchers.IO) {
            val list = BaseApplication.getDB().HistoryDao().findAll()
            list.forEach {
                binding.flHomeSearchHistory.addView(generateTextView(it.keyword))
            }
        }
    }

    private fun generateTextView(txt: String): TextView {
        val textView: TextView = TextView(this)
        textView.setText(txt)
        textView.layoutParams =
            ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        textView.setPadding(padding, padding, padding, padding)
        textView.background = resources.getDrawable(R.drawable.titlebartxt_border)

        textView.setOnClickListener {
            routeSearchResultActivity(txt)
        }

        return textView
    }

    override fun initEvent() {
        binding.include.btnHomeSearchSearch.setOnClickListener {
            val keyword: String = binding.include.etHomeSearchContent.text.trim().toString()
            if (keyword.isBlank()) {
                showMsg(getString(R.string.home_search_pleaseinputkeyword))
                return@setOnClickListener
            }
            generateLifeCycleLaunch {
                withContext(Dispatchers.IO) {
                    BaseApplication.getDB().HistoryDao().addHistory(HistoryEntity(0, keyword))
                    routeSearchResultActivity(keyword)
                }

            }
        }
    }

    private fun routeSearchResultActivity(keyword:String){
        val bundle = Bundle()
        bundle.putString("keyword",keyword)
        route(SearchResultActivity::class.java,bundle)
        closeThiz()
    }
}